﻿using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Repository;
using ZR.Service.IService.ProductionControl;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 报警信息配置表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksDeviceRunRecordService), ServiceLifetime = LifeTime.Transient)]
    public class CksDeviceRunRecordService : BaseService<CksDeviceRunRecord>, ICksDeviceRunRecordService {
        /// <summary>
        /// 查询设备运行记录列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksDeviceRunRecordDto> GetList(CksDeviceRunRecordQueryDto parm) {
            //var predicate = QueryExp(parm);

            var response = Queryable()
                .LeftJoin<PlcConfig>((recDt, plcDt) => recDt.PlcId == plcDt.Id)
                .WhereIF(!string.IsNullOrEmpty(parm.LineCode), (recDt, plcDt) => plcDt.ProductionLineCode == parm.LineCode)
                .WhereIF(!string.IsNullOrEmpty(parm.StationCode), (recDt, plcDt) => plcDt.WorkstationCode == parm.StationCode)
                .WhereIF(parm.BeginTime != null && parm.EndTime != null, (recDt, plcDt) => recDt.CreateTime >= parm.BeginTime && recDt.CreateTime <= parm.EndTime)
                .OrderByDescending((recDt, plcDt) => recDt.Id)
                .Select((recDt, plcDt) => new CksDeviceRunRecordDto {
                    Id = recDt.Id,
                    PlcId = recDt.PlcId,
                    DbBlock = recDt.DbBlock,
                    StationCode = plcDt.WorkstationCode,
                    ProductionLineCode = plcDt.ProductionLineCode,
                    StartTime = recDt.StartTime,
                    EndTime = recDt.EndTime,
                    Duration = recDt.Duration,
                    EffectType = recDt.EffectType,
                    EffectReason = recDt.EffectReason,
                    CreateTime = recDt.CreateTime
                }).ToPage(parm);
            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksDeviceRunRecord GetInfo(int Id) {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加设备运行记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksDeviceRunRecord AddCksDeviceRunRecord(CksDeviceRunRecord model) {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改设备运行记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksDeviceRunRecord(CksDeviceRunRecord model) {
            return Update(model, true);
        }

        /// <summary>
        /// 清空设备运行记录
        /// </summary>
        /// <returns></returns>
        public bool TruncateCksDeviceRunRecord() {
            var newTableName = $"cks_device_run_rec_{DateTime.Now:yyyyMMdd}";
            if (Queryable().Any() && !Context.DbMaintenance.IsAnyTable(newTableName)) {
                Context.DbMaintenance.BackupTable("cks_device_run_rec", newTableName);
            }

            return Truncate();
        }

        ///// <summary>
        ///// 查询导出表达式
        ///// </summary>
        ///// <param name="parm"></param>
        ///// <returns></returns>
        //private static Expressionable<CksDeviceRunRecord> QueryExp(CksDeviceRunRecordQueryDto parm) {
        //    var predicate = Expressionable.Create<CksDeviceRunRecord>();

        //    return predicate;
        //}
    }

}
